package com.iprojectmanager.businessclass;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.iprojectmanager.managers.*;
import com.iprojectmanager.entities.*;

/**
 * @author Trejo Sandoval Sergio Alberto
 */
public class UserControl extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException 
    {
        String login = request.getParameter("user");
        String password = request.getParameter("password");
        
            UserManager manager = new UserManager();
            Usuario user = manager.login(login, password);
            
                if(user != null)
                {
                    HttpSession session = request.getSession(true);
                    session.setAttribute("user", user);
                    if(user instanceof LiderProyecto)
                    {
                        RequestDispatcher disp = request.getRequestDispatcher("VistaLider");
                        disp.forward(request, response);
                    }
                    if(user instanceof IntegranteProyecto)
                    {
                        RequestDispatcher disp = request.getRequestDispatcher("VistaUsuario");
                        disp.forward(request, response);
                    }
                    if(user instanceof Administrador)
                    {
                        RequestDispatcher disp = request.getRequestDispatcher("VistaAdministrador");
                        disp.forward(request, response);
                    }
                }else if(login == null && password == null)
                {
                    request.getSession().invalidate();
                    request.setAttribute("Error","");
                    RequestDispatcher disp = request.getRequestDispatcher("inicio");
                    disp.forward(request, response);
                }
                else
                {
                    request.setAttribute("Error","Usuario y/o contraseña invalida");
                    RequestDispatcher disp = request.getRequestDispatcher("inicio");
                    disp.forward(request, response);
                }
    }


    @Override
    public String getServletInfo() {
        return "Clase de Control para el proceso de Autenticacion";
    }
}
